package com.zwz.service.staticConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zwz.bean.SysRolesBean;
import com.zwz.dao.SysRoleResourceDao;
import com.zwz.model.SysRoleResource;
import com.zwz.model.SysRoles;
import com.zwz.service.baseConfig.SysRoleService;

/**
 * 初始化角色权限（后期换成缓存）
 * @author zhangwanzhong
 * 2016年3月3日 下午2:52:29
 */
@Service
public class RoleAuthService {
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysRoleResourceDao<SysRoleResource> sysRoleResourceDao; 
	
	private static Map<String, List<String>> roleAuths;
	private RoleAuthService(){}
	
	/**
	 * 初始化角色权限链接
	 * @return
	 */
	@PostConstruct
	public Map<String, List<String>> initRoleAuth(){
		if(roleAuths==null){
			roleAuths = new HashMap<String, List<String>>();
		}
		//获得全部角色
		SysRolesBean model=new SysRolesBean();
		model.setRows(1000);
		List<SysRoles> roles = sysRoleService.queryByList(model);
		//根据角色获取url
		for (SysRoles role : roles) {
			List<String> urls = sysRoleResourceDao.getUrlByRoleId(role.getId());
			roleAuths.put(role.getRole_code(), urls);
		}
		return roleAuths;
	}
	
	/**
	 * 获取全部角色对应的URL
	 * @return
	 */
	public static List<String> getRoleAuth(String roleCode){
		return roleAuths.get(roleCode);
	}
}
